﻿namespace DependencyPropertyChangedSample
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Interactivity;
    using System.Windows.Media.Animation;
    using DependencyPropertyChangedSample.Behaviors;
    using DependencyPropertyChangedSample.ComponentModel;
    using Microsoft.Phone.Controls;

    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void buttonAttach_Click(object sender, RoutedEventArgs ex)
        {
            if (this.FindName("dyn") == null)
            {
                var dyn = new Image()
                {
                    Name = "dyn",
                    Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/cover.png", UriKind.RelativeOrAbsolute))
                };


                canvas.Children.Add(dyn);

                DependencyPropertyChangedEventHandler handler = (o, e) =>
                {
                    MessageBox.Show("Old Value: " + e.OldValue + "\nNew Value: " + e.NewValue, (o as DependencyObject).GetValue(FrameworkElement.NameProperty).ToString(), MessageBoxButton.OK);
                };

                DependencyPropertyChangedHandlerAttacher.Register(buttonLeft, "(Canvas.Left)", handler);
                DependencyPropertyChangedHandlerAttacher.Register(buttonTop, "(Canvas.Top)", handler);
                DependencyPropertyChangedHandlerAttacher.Register(dyn, "(Canvas.Left)", handler);

                buttonAttach.IsEnabled = false;
                buttonAnimate.IsEnabled = buttonDrag.IsEnabled = true;
            }
        }

        private void buttonTop_Click(object sender, RoutedEventArgs e)
        {
            Canvas.SetTop(sender as UIElement, Canvas.GetTop(sender as UIElement) - 20);
        }

        private void buttonLeft_Click(object sender, RoutedEventArgs e)
        {

            Canvas.SetLeft(sender as UIElement, Canvas.GetLeft(sender as UIElement) - 20);
        }

        private void buttonAnimate_Click(object sender, RoutedEventArgs e)
        {
            var element = this.FindName("dyn") as DependencyObject;

            var animation = new PointAnimation()
            {
                Duration = new Duration(TimeSpan.FromSeconds(2)),
                From = new Point(10, 10),
                To = new Point(200, 200),
                AutoReverse = true
            };

            var helper = new DependencyPropertyDistributionHelper<Point>(element, (o, p) =>
            {
                Canvas.SetLeft((o as UIElement), p.X);
                Canvas.SetTop((o as UIElement), p.Y);
            });

            helper.SetTargets(animation);

            Storyboard s = new Storyboard();
            s.Children.Add(animation);
            s.Begin();
        }

        private void buttonDrag_Click(object sender, RoutedEventArgs e)
        {
            var dfb = new DragFlickBehavior();

            var element = this.FindName("dyn") as FrameworkElement;

            dfb.TargetControls.Add(element.Name);

            var bc = Interaction.GetBehaviors(element);

            bc.Add(dfb);

            dfb.Load();
        }
    }
}